<template>
  <el-dialog
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    :title="title"
    width="650px"
    @close="handleCancel">
    <div class="add-header">
      <el-button
        type="primary"
        @click="addClick">新建打印模板</el-button>
    </div>
    <div class="add-wrap">
      <el-table
        height="300px"
        :data="tableData"
        border
        :cell-class-name="cellClassName"
        @row-click="handleRowClick">
        <el-table-column
          v-for="(item, index) in fieldList"
          :key="index"
          :prop="item.prop"
          :min-width="item.width"
          :label="item.label"
          show-overflow-tooltip />
        <el-table-column
          fixed="right"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button
              type="primary-text"
              style="padding: 0;"
              @click="handleClick('edit', scope)">编辑名称</el-button>
            <el-button
              type="primary-text"
              style="padding: 0;"
              @click="handleClick('delete', scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 日志类型添加 -->
    <template-type-add
      :visible.sync="templateAddShow"
      :detail="editData"
      @save="getList"
      @next="createNext" />
  </el-dialog>
</template>

<script>
import {
  oaLogPrintTemplateListAPI,
  oaLogPrintDeleteTemplateAPI
} from '@/api/admin/oa'

import TemplateTypeAdd from '@/views/admin/oa/logPrintTemplates/components/TemplateTypeAdd'

export default {
  // OwnPrintDialog
  name: 'OwnPrintDialog',

  components: {
    TemplateTypeAdd
  },

  props: {
    visible: {
      type: Boolean,
      required: true,
      default: false
    }
  },

  data() {
    return {
      tableData: [],
      fieldList: [
        {
          prop: 'templateName',
          label: '模板名称',
          width: 150
        },
        {
          prop: 'configName',
          label: '关联对象',
          width: 150
        },
        {
          prop: 'createTime',
          label: '创建时间',
          width: 150
        },
        {
          prop: 'createUserName',
          label: '创建人',
          width: 150
        },
        {
          prop: 'updateTime',
          label: '更新时间',
          width: 150
        }
      ],
      // 新建
      editData: null,
      templateAddShow: false
    }
  },

  computed: {
    title() {
      return '我的打印模板'
    }
  },

  watch: {},

  created() {
    this.getList()
  },

  mounted() {},

  beforeDestroy() {},

  methods: {
    /**
     * 提交按钮
     */
    getList() {
      this.loading = true
      oaLogPrintTemplateListAPI({
        pageType: 0, // 不分页
        type: 1 // 0查询管理员配置的，1查询个人的，2查询全部的
      }).then(res => {
        this.tableData = res.data.list || []
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },

    /**
     * 通过回调控制class
     */
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'templateName') {
        return 'can-visit--underline'
      } else {
        return ''
      }
    },

    /**
     * 当某一行被点击时会触发该事件
     */
    handleRowClick(row, column, event) {
      if (column.property === 'templateName') {
        this.$router.push({
          name: 'oaLogPrintDetail',
          query: {
            handle: 'detail',
            templateName: row.templateName,
            templateId: row.templateId,
            configId: row.configId,
            configName: row.configName
          }
        })
      }
    },

    /**
     * @description: 新建逻辑
     * @return {*}
     */
    addClick() {
      this.editData = null
      this.templateAddShow = true
    },

    /**
     * @description: 新建下一步
     * @param {*} data
     * @return {*}
     */
    createNext(data) {
      this.$router.push({
        name: 'oaLogPrintDetail',
        query: {
          handle: 'create',
          systemFlag: '0',
          ...data
        }
      })
    },

    /**
     * @description: 行操作
     * @param {*} type
     * @param {*} scope
     * @return {*}
     */
    handleClick(type, scope) {
      if (type === 'edit') {
        this.editData = scope.row
        this.templateAddShow = true
      } else if (type === 'delete') {
        // 启用停用
        this.$confirm('确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.loading = true
            oaLogPrintDeleteTemplateAPI({
              templateId: scope.row['templateId']
            })
              .then(res => {
                this.getList()
                this.$message({
                  type: 'success',
                  message: '操作成功'
                })
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
          })
          .catch(() => {})
      }
    },

    /**
     * 取消选择
     */
    handleCancel() {
      // custom-class="no-padding-dialog"
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding: 0 20px;
}

.add-header {
  line-height: 30px;
  text-align: right;
}

.add-wrap {
  .el-table {
    width: 100%;
    margin-top: 16px;
  }

  padding-bottom: 16px;
}
</style>
